package com.ysd.lis.util.qcrule;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class AXRule extends Rule{
    public AXRule(String id, String name, int a, int n, double l) {
        super(id, name, a, n, l);
        this.type = RuleType.AXRule;
    }

    @Override
    public RuleStatement Evaluate(QCResult result) {
        //无数据,失败
        //if (!result.Results.Any())
        if(CollectionUtils.isEmpty(result.getResults()))
        {
            return RuleStatement.FAILED;
        }

        if (result.getResults().size() < a)
        {
            return RuleStatement.FAILED;
        }

        List<Result> results = result.getResults();
        //var items = result.Results.GetRange(result.Results.Count - A, A);
        List<Result> items = new ArrayList<>();
        for(int i = 0 ;i< a; i++){
            items.add(results.get(results.size()-a+i));
        }

        if (HasEmptyResult(items))
        {
            return RuleStatement.DISABLE;
        }

        //var hs = items.Where(p => p.Z > 0).ToList();
        List<Result> hs = items.stream().filter(p -> (p.getZvalue() > 0)).collect(Collectors.toList());

        //var ls = items.Where(p => p.Z < 0).ToList();
        List<Result> ls = items.stream().filter(p -> (p.getZvalue() < 0)).collect(Collectors.toList());

        if (hs.size() >= a || ls.size() >= a)
        {

            //items.Last().IsOutControl = true;
            items.get(items.size()-1).setIsOutControl(true);
            //items.Last().OutControlInfos.Add(new OutControlInfo()
            items.get(items.size()-1).getOutControlInfos().add(new OutControlInfo(id,false,
                    hs.size()>=a?hs.stream().map(Result::getId).collect(Collectors.toList()) : ls.stream().map(Result::getId).collect(Collectors.toList())));
//            {
//                RuleId = Id,
//                IsMutiMaterial = false,
//                MemberIds = hs.Count() >= A ? hs.Select(p => p.Id).ToList() : ls.Select(p => p.Id).ToList()
//            });
            return RuleStatement.OK;
        }


        return RuleStatement.FAILED;
    }

    @Override
    public QCResult ListToQCResult(List<QCResult> results) {
        if (CollectionUtils.isEmpty(results))
        {
            return null;
        }

        //最少数据个数*质控物 小于需要的数据量
        //if (results.Select(p => p.Results.Count).Min() * results.Count() < A)
        List<QCResult> collect = results.stream().filter(re -> (re.getResults().size() * results.size()) < a).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(collect))
        {
            return null;
        }

        //不能整除,失败
        if (a % results.size() > 0)
        {
            return null;
        }

        int count = a / results.size();

        QCResult result = new QCResult();

        //获取必要数据
        for (QCResult r : results)
        {
            if (r.getResults().size() < count)
            {
                return null;
            }


            List<Result> res = r.getResults();
            //var items = result.Results.GetRange(result.Results.Count - A, A);
            List<Result> items = new ArrayList<>();
            for(int i = 0 ;i< count; i++){
                items.add(res.get(res.size()-count+i));
            }
            result.getResults().addAll(items);
        }

        return result;
    }
}
